<template>
  <div class="login-register-container">
    <div class="form-box">
      <h2 class="title">用户注册</h2>
      <el-form
        ref="registerForm2"
        :model="registerForm"
        :rules="registerRules"
        label-width="80px"
        class="register-form"
      >
        <el-form-item label="用户名" prop="username">
          <el-input
            v-model="registerForm.username"
            placeholder="请输入用户名"
            clearable
          />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            v-model="registerForm.password"
            type="password"
            placeholder="请输入密码"
            clearable
          />
        </el-form-item>
        <el-form-item label="确认密码" prop="confirmPassword">
          <el-input
            v-model="registerForm.confirmPassword"
            type="password"
            placeholder="请确认密码"
            clearable
          />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            class="submit-btn"
            @click="handleRegister"
          >
            注册
          </el-button>
        </el-form-item>
        <div class="to-login">
          已有账号？
          <el-link type="primary" @click="$router.push('/login')">立即登录</el-link>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { useRouter } from 'vue-router'
import axios from '../utils/axios' 
const registerForm2 = ref()
const router = useRouter()
const registerForm = reactive({
  username: '',
  password: '',
  confirmPassword: ''
})
const registerRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 16, message: '用户名长度在 3 到 16 个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 16, message: '密码长度在 6 到 16 个字符', trigger: 'blur' }
  ],
  confirmPassword: [
    { required: true, message: '请确认密码', trigger: 'blur' },
    {
      validator: (rule, value, callback) => {
        if (value!== registerForm.password) {
          callback(new Error('两次输入密码不一致'))
        } else {
          callback()
        }
      },
      trigger: 'blur'
    }
  ]
}

const handleRegister = async () => {
  try {
    await registerForm2.value.validate()
    const res = await axios.post('/user/register', {
      username: registerForm.username,
      password: registerForm.password
    })
    if (res.code === 200) {
      ElMessage.success('注册成功，请前往登录')
      // 注册成功后可跳转到登录页
      router.push('/login')
    } else {
      ElMessage.error(res.msg || '注册失败，请稍后再试')
    }
  } catch (error) {
    if (error instanceof Error) {
      ElMessage.error(error.message)
    } else {
      ElMessage.error('注册出错，请检查输入')
    }
  }
}
</script>

<style scoped>
.login-register-container {
  width: 100vw;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background: url('../assets/login.jpg') center/cover no-repeat; 
}
.form-box {
  width: 400px;
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
  padding: 24px;
}
.title {
  text-align: center;
  font-size: 20px;
  font-weight: bold;
  color: #e63946;
  margin-bottom: 24px;
}
.register-form {
  margin-top: 16px;
}
.submit-btn {
  width: 100%;
  background-color: #e63946;
  border: none;
}
.submit-btn:hover {
  background-color: #c92a36;
}
.to-login {
  text-align: center;
  margin-top: 16px;
  color: #666;
}
.to-login a {
  color: #e63946;
  text-decoration: none;
}
</style>